﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Projektet.Model
{
    class Catapult
    {
        public Vector2 _position;
        public ExplosionListener _explosionListener;

        public int _width;
        public int _height;
        
        public float _power = 0f;
        private float _maxPower = 0.1f;
        public float _cooldown = 0f;
        public float _reloadSpeed = 0.2f;
        
        public List<CatapultShot> _projectileList = new List<CatapultShot>();

        public Catapult(Vector2 position)
        {
            _position = new Vector2(position.X + 5f, position.Y - 135f);
            _width = 5;
            _height = 5;
        }

        public void Update(GameTime gameTime, List<CombatUnit> unitList)
        {
            if (_cooldown < 1f)
            {
                _cooldown += (float)gameTime.ElapsedGameTime.TotalSeconds * _reloadSpeed;
            }

            for (int i = _projectileList.Count; i > 0; i--)
            {
                _projectileList[i - 1].Update(gameTime);
                _projectileList[i - 1].DidHit(_explosionListener, unitList);
                if (_projectileList[i - 1].ShotExpired(_explosionListener))
                {
                    _projectileList.RemoveAt(i - 1);
                }
            }         
        }

        public void Shoot() 
        {
            if (_cooldown >= 1f)
            {
                Vector2 velocity = new Vector2(_power * _maxPower, -(_power * 5) * _maxPower);
                _projectileList.Add(new CatapultShot(_position, velocity));
                _cooldown = _reloadSpeed;
            }
        }

        public void IncreasePower() 
        {
            if (_power > 1)
            {
                _power = 1f;
            }
            if(_power < 1)
            {
                _power += 0.01f;
            }
        }

        public void DecreasePower() 
        {
            if(_power < 0)
            {
                _power = 0f;
            }
            if (_power > 0)
            {
                _power -= 0.01f;
            }
        }
    }
}
